package team.fivez.defaultmanage.dao.Impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import team.fivez.defaultmanage.dao.RedoApplicationDao;
import team.fivez.defaultmanage.domain.IndustryInfo;
import team.fivez.defaultmanage.domain.RedoApplication;
import team.fivez.defaultmanage.domain.RegionInfo;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Repository
public class RedoApplicationDaoImpl implements RedoApplicationDao {


    @Resource
    private JdbcTemplate jdbcTemplate;


    //插入申请表
    @Override
    public int insertRedoApplication(RedoApplication redoApplication) {
        String sql="insert into redo_application(" +
                "application_number,severity,approvement_status,application_time," +
                "applicant_account,customer_name,redo_number) values(?,?,?,?,?,?,?)";
        String applicationNumber = redoApplication.getApplicationNumber();
        String severity = redoApplication.getSeverity();
        int approvementStatus=0;
        Timestamp applicationTime = redoApplication.getApplicationTime();
        String applicantAccount = redoApplication.getApplicantAccount();
        String customerName = redoApplication.getCustomerName();
        int redoNumber = redoApplication.getRedoNumber();

        return  jdbcTemplate.update(sql,applicationNumber,severity,approvementStatus,applicationTime,
                                    applicantAccount,customerName,redoNumber);
    }

    //根据客户名查找重生申请单信息
    @Override
    public RedoApplication getRedoApplicationByCustomerName(String customerName) {

        String sql="select * from redo_application where customer_name=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(RedoApplication.class),customerName);
    }


    //根据审核状态查看申请单信息列表
    @Override
    public List<RedoApplication> getListByApprovementStatusAndApplicantAccount(int approvementStatus, String applicantAccount) {
        String sql="select * from redo_application where approvement_status=? and applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class),approvementStatus,applicantAccount);
    }

    //根据客户名称和申请人查看重生申请单列表
    @Override
    public List<RedoApplication> getListByCustomerNameAndApplicantAccount(String customerName, String applicantAccount) {
        String sql="select * from redo_application where customer_name like ? and applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class),"%"+customerName+"%",applicantAccount);
    }

    //全部的重生违约单
    @Override
    public List<RedoApplication> getAllList() {
        String sql="select * from redo_application";
        return  jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class));

    }

    //根据审核状态查找重生单信息列表
    @Override
    public List<RedoApplication> getListByApprovementStatus(int approvementStatus) {

        String sql="select * from redo_application where approvement_status=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class),approvementStatus);
    }

    //根据申请人账号获取所有的申请表
    @Override
    public List<RedoApplication> getListByApplicantAccount(String applicantAccount) {
        String sql="select * from redo_application where applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class),applicantAccount);

    }

    //根据客户名称查找重生单信息列表
    @Override
    public List<RedoApplication> getListByCustomerName(String customerName) {
        String sql="select * from redo_application where customer_name like ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RedoApplication.class),"%"+customerName+"%");

    }

    //更改审核状态
    @Override
    public void updateApprovementStatusByApplicationNumber(int approvementStatus, String applicationNumber) {
        String sql="update redo_application set approvement_status=? where application_number=?";
        jdbcTemplate.update(sql,approvementStatus,applicationNumber);
    }

    //根据申请单号进行查找
    @Override
    public RedoApplication getRedoApplicationByNumber(String number) {
        String sql="select * from redo_application where application_number=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(RedoApplication.class),number);
    }

    //更新审核时间
    @Override
    public void updateApplicationTimeByApplicationNumber(String applicationNumber, Timestamp approvementTime) {
        String sql="update redo_application set approvement_time=? where application_number=?";
        jdbcTemplate.update(sql,approvementTime,applicationNumber);
    }

    //更新审核人姓名
    @Override
    public void updateDefaultApproverNameByApplicationNumber(String applicationNumber, String redoApproverName) {
        String sql="update redo_application set redo_approver_name=? where application_number=?";
        jdbcTemplate.update(sql,redoApproverName,applicationNumber);
    }

    @Override
    public Integer countByName(String name) {
        String sql="select count(*) from redo_application where customer_name=? and approvement_status=0";
        return jdbcTemplate.queryForObject(sql,Integer.class,name);
    }

    @Override
    public List<IndustryInfo> getRedoIndustryInfo() {
        String sql="select * from industry_redo_V";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(IndustryInfo.class));
    }

    @Override
    public List<RegionInfo> getRedoRegionInfo() {
        String sql="select * from region_redo_V";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RegionInfo.class));
    }


}
